package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.media.AliyunVod;
import com.xuecheng.api.media.aliyun.GetAliyunVodToken;
import com.xuecheng.api.media.aliyun.RespAliyunVodToken;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * <h4>xuecheng</h4>
 * <p>媒资文件上传凭证</p>
 *
 * @author : by wujianming
 * @date : 2021-12-14 19:35
 **/
@RestController
@Slf4j
public class AliyunVodController implements AliyunVod {

    //aliyun.region = cn-shanghai
    //aliyun.accessKeyId = LTAI5tC9bKCcJuc11yKfiFdc
    //aliyun.accessKeySecret = PSh3hoPGEI4NRtPAvv8UTeCxRVJ69y
    //注入Nacos配置
    @Value("${aliyun.region}")
    private String region;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    //获取阿里云媒资上传凭证
    @Override
    @PostMapping("media/vod-token")
    public RespAliyunVodToken respAliyunVod(@RequestBody GetAliyunVodToken getAliyunVodToken) {

        try {
            //调用工具类初始化客户端
            //只要和本项目中的微服务之外的内容产生交互.异常必须要try--catch处理
            //原因网络不稳定
            DefaultAcsClient defaultAcsClient = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);

            //创建请求对象并获得响应
            CreateUploadVideoResponse uploadVideo = AliyunVODUtil.createUploadVideo(defaultAcsClient, getAliyunVodToken.getTitle(), getAliyunVodToken.getFileName());

            //创建响应的对象
            RespAliyunVodToken token = new RespAliyunVodToken();

            //封装响应结果集
            token.setVideoId(uploadVideo.getVideoId());
            token.setRequestId(uploadVideo.getRequestId());
            token.setUploadAddress(uploadVideo.getUploadAddress());
            token.setUploadAuth(uploadVideo.getUploadAuth());
//            token.setUserId(uploadVideo.getUserId());
            //返回结果集
            return token;

        } catch (Exception e) {
//            e.printStackTrace(); //不能写-->高并发大量写进日志io量大.造成系统崩坏---》离职
            String message = e.getMessage();
            log.error(message);
            ExceptionCast.cast(CommonErrorCode.CUSTOM);
            //编译器只能识别new 的异常,不能识别调用的异常.所以需要加上return null
            return null;
        }
    }

    //刷寻阿里云媒资上传文件凭证
    @Override
    @GetMapping("media/refresh-vod-token/{videoId}")
    public RespAliyunVodToken refreshAliyunVod(@PathVariable String videoId) {

        try {
            //调用工具类初始化客户端
            //只要和本项目中的微服务之外的内容产生交互.异常必须要try--catch处理
            //原因网络不稳定
            DefaultAcsClient defaultAcsClient = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);


//            CreateUploadVideoResponse uploadVideo = AliyunVODUtil.createUploadVideo(defaultAcsClient, getAliyunVodToken.getTitle(), getAliyunVodToken.getFileName());

            //调用工具类刷新凭证返回
            RefreshUploadVideoResponse uploadVideo = AliyunVODUtil.refreshUploadVideo(defaultAcsClient, videoId);
            //创建响应的对象
            RespAliyunVodToken token = new RespAliyunVodToken();

            //封装响应结果集
            token.setVideoId(uploadVideo.getVideoId());
            token.setRequestId(uploadVideo.getRequestId());
            token.setUploadAddress(uploadVideo.getUploadAddress());
            token.setUploadAuth(uploadVideo.getUploadAuth());
//            token.setUserId(uploadVideo.getUserId());
            //返回结果集
            return token;

        } catch (Exception e) {
//            e.printStackTrace(); //不能写-->高并发大量写进日志io量大.造成系统崩坏---》离职
            String message = e.getMessage();
            log.error(message);
            ExceptionCast.cast(CommonErrorCode.CUSTOM);
            //编译器只能识别new 的异常,不能识别调用的异常.所以需要加上return null
            return null;
        }
    }


}
